package org.sith.taskrunner.gwt.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import org.sith.taskrunner.gwt.client.event.*;
import org.sith.taskrunner.gwt.client.resources.TaskRunnerMessages;
import org.sith.taskrunner.gwt.client.services.user.UsersManageGWTService;
import org.sith.taskrunner.gwt.client.view.ITableView;
import org.sith.taskrunner.gwt.client.view.IUserTableView;
import org.sith.taskrunner.gwt.client.view.UserManageWidget;
import org.sith.taskrunner.gwt.shared.proxy.UserEntityProxy;

import java.util.List;

public class UsersTablePresenter implements TabPresenter, IUserTableView.Presenter {
    private HasWidgets container;
    private IUserTableView view;
    private EventBus eventBus;


    public UsersTablePresenter(IUserTableView view, EventBus eventBus) {
        this.view = view;
        this.eventBus = eventBus;

        bind();
    }

    private void bind() {

        eventBus.addHandler(UsersTableEvent.TYPE, new UsersTableEventHandler() {
            @Override
            public void onUpdateAllUsers(UsersTableEvent event) {
                onUpdateAllUsers(event);
            }

            @Override
            public void onUserUpdated(UsersTableEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        eventBus.addHandler(UserManageEvent.TYPE,new UserManageEventHandler() {
            @Override
            public void onUserAdded(UserManageEvent event) {

            }

            @Override
            public void onUserUpdated(UserManageEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onUserActiveStateUpdated(UserManageEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onUserRemoved(UserManageEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void onUserRoleUpdated(UserManageEvent event) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });


    }

    @Override
    public void go(HasWidgets container) {
        this.container = container;
        this.container.add(view.getViewWidget());
        updateTableData();
    }

    private void updateTableData() {
        UsersManageGWTService.App.getInstance().getUsers(new AsyncCallback<List<UserEntityProxy>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error happend on load users: " + caught);
            }

            @Override
            public void onSuccess(List<UserEntityProxy> result) {
                view.setTableData(result);
            }
        });
    }

    @Override
    public void reloadUsers() {
        updateTableData();
    }

    @Override
    public void editUser(UserEntityProxy user) {
        UserManageWidget userManageWidget = new UserManageWidget();
        UserManagePresenter userManagePresenter = new UserManagePresenter(userManageWidget, eventBus);
        userManageWidget.setPresenter(userManagePresenter);
        userManageWidget.setEditedObject(user);


    }

    @Override
    public void addUser() {
        GuiActionEvent event = new GuiActionEvent();
        event.setEventType(GuiActionEvent.EventType.ADD_USER);
        eventBus.fireEvent(event);
    }

    @Override
    public void removeUser(UserEntityProxy selectedObject) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getTabTitle() {
        return TaskRunnerMessages.App.getInstance().usersTabLabel();
    }
}
